﻿@page "/gateways"

@using Gurux.DLMS.AMI.Shared.DIs

@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs
@using Gurux.DLMS.AMI.Shared.Rest
@using Microsoft.AspNetCore.SignalR.Client
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Client.Helpers
@using Gurux.DLMS.AMI.Module
@using System.Globalization

@attribute [Authorize(Roles = "Admin, GatewayManager")]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier
@implements IDisposable
@implements IAmiComponent
@if (Title)
{
    <PageTitle>@Properties.Resources.Gateways</PageTitle>
}

<AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.GatewayManager)>
    <MenuControl RightCorner="true">
        <ChildContent>
            <MenuItem Text="@Properties.Resources.Add" Icon="oi oi-plus" OnClick="@OnAdd" />
            <MenuItem Text="@Properties.Resources.Remove" Icon="oi oi-trash" OnClick="@OnRemove" />
        </ChildContent>
    </MenuControl>
</AuthorizeView>

<GXTable @ref="table" Context="gateway" 
    ItemsProvider="@GetItems" 
    SelectionMode="SelectionMode.Multiple" 
    Filter="@Filter"
         ShowAllUsers="@Header"
         ShowRemoved="@Header"
         Columns="@Columns"
         CanEdit="@CanEdit"
         OnSearch="@Updated">
    <FilterContent>
        <th>
            <input class="form-control" type="text" placeholder="@Properties.Resources.FilterByName"
                   @onchange="@((ChangeEventArgs __e) => filter.Name = Convert.ToString(__e.Value))" />
        </th>
        <th>
            <input class="form-control" placeholder="按创建时间..."
                   type="datetime-local"
                   @onchange="@((ChangeEventArgs __e) => filter.CreationTime = "".Equals(__e.Value) ? DateTime.MinValue : Convert.ToDateTime(__e.Value))" />
        </th>
        <th>
            <input class="form-control" placeholder="按更新时间..."
                   type="datetime-local"
            @onchange="@((ChangeEventArgs __e) => filter.Updated = "".Equals(__e.Value) ? null :Convert.ToDateTime(__e.Value))" />
        </th>
        <th>
            <input class="form-control" placeholder="Filter by detection time..."
                   type="datetime-local"
                   @onchange="@((ChangeEventArgs __e) => filter.Detected = "".Equals(__e.Value) ? null :Convert.ToDateTime(__e.Value))" />
        </th>
        <th>
            <!--Status-->
        </th>
    </FilterContent>
    <MenuContent>
        <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.GatewayManager)>
            <ContextMenuItem Text="@Properties.Resources.Run" Icon="oi oi-flash" OnClick="async () => OnRun()"></ContextMenuItem>
            <ContextMenuItem Text="@Properties.Resources.Edit" Icon="oi oi-pencil" OnClick="@OnEdit"></ContextMenuItem>
            <ContextMenuItem Text="@Properties.Resources.Remove" Icon="oi oi-trash" OnClick="@OnRemove"></ContextMenuItem>
        </AuthorizeView>
    </MenuContent>
    <HeaderContent>
        <Th Id="Name">@Properties.Resources.Name</Th>
        <Th Id="CreationTime" SortMode="SortMode.Descending">@Properties.Resources.CreationTime</Th>
        <Th Id="Updated">@Properties.Resources.Updated</Th>
        <Th Id="ExecutionTime">@Properties.Resources.Detected</Th>
        <Th Id="Status">@Properties.Resources.Status</Th>
    </HeaderContent>
    <ItemContent>
        <Td Link="@("Gateway/Edit/" + gateway.Id)">@gateway.Name</Td>
        <td>@gateway.CreationTime</td>
        <td>@gateway.Updated</td>
        <td>@gateway.Detected</td>
        <td>@gateway.Status</td>
    </ItemContent>
</GXTable>
<br />
<Confirm @ref="DeleteConfirmation"
         ConfirmationChanged="OnDeleteConfirmation"
         OkTitle="@Properties.Resources.Remove"
         ConfirmationMessage=""
         ConfirmationTitle="@Properties.Resources.AreYouSureYouWantToDeleteSelectedItems">
</Confirm>

@code {
    /// <summary>
    /// Selected gateway ID.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }

    /// <inheritdoc />
    public string Name
    {
        get
        {
            return Gurux.DLMS.AMI.Client.Properties.Resources.Gateways;
        }
    }

    /// <inheritdoc />
    public Type? ConfigurationUI
    {
        get
        {
            return null;
        }
    }

    /// <inheritdoc />
    public string? Icon
    {
        get
        {
            return "oi oi-timer";
        }
    }

    /// <summary>
    /// Amount of the gateways shown on the view.
    /// </summary>
    [Parameter]
    public int Count { get; set; } = 0;

    /// <summary>
    /// Is filter shown.
    /// </summary>
    [Parameter]
    public bool Filter { get; set; } = true;

    /// <summary>
    /// Is header shown.
    /// </summary>
    [Parameter]
    public bool Header { get; set; } = true;

    /// <summary>
    /// Is edit allowed.
    /// </summary>
    [Parameter]
    public bool CanEdit { get; set; } = true;

    /// <summary>
    /// Available columns.
    /// </summary>
    [Parameter]
    public string[]? Columns { get; set; }

    /// <summary>
    /// Is title shown.
    /// </summary>
    [Parameter]
    public bool Title { get; set; } = true;

    /// <summary>
    /// Gateway filter.
    /// </summary>
    private GXGateway filter = new GXGateway();
    /// <summary>
    /// User is verified before gateway is removed.
    /// </summary>
    protected ConfirmBase? DeleteConfirmation;

    /// <summary>
    /// Table reference.
    /// </summary>
    protected GXTable<GXGateway>? table;

    /// <summary>
    /// Update table.
    /// </summary>
    protected async Task Updated()
    {
        try
        {
            if (table != null)
            {
                Notifier?.ClearStatus();
                await table.RefreshDataAsync(true);
            }
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    protected override async Task OnInitializedAsync()
    {
        //Wait until table is loaded. Don't remove this or table is null and last settings are not available.
        await Task.Delay(1);
        try
        {
            if (Notifier == null)
            {
                throw new ArgumentException(Properties.Resources.InvalidNotifier);
            }
            Notifier.ProgressStart();
            Notifier.ClearStatus();
            Notifier.On<IEnumerable<GXGateway>>(this, nameof(IGXHubEvents.GatewayUpdate), async (gateway) =>
            {
                await Updated();
            });
            Notifier.On<IEnumerable<GXGateway>>(this, nameof(IGXHubEvents.GatewayDelete), (gateways) =>
            {
                table?.RemoveItems(gateways);
            });
            //Clear buttons.
            Notifier.Clear();
            Notifier.UpdateButtons();
            if (table != null && Id != null)
            {
                //Get last selected item.
                table.Active = new GXGateway() { Id = Id.Value };
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    private async ValueTask<ItemsProviderResult<GXGateway>> GetItems(GXItemsProviderRequest request)
    {
        //Don't clear status or error is lost.
        Notifier?.ProgressStart();
        try
        {
            if (request.Removed)
            {
                filter.Removed = DateTimeOffset.MaxValue;
            }
            else
            {
                filter.Removed = null;
            }
            ListGateways req = new ListGateways()
                {
                    Index = request.StartIndex,
                    Count = request.Count,
                    Filter = filter,
                    OrderBy = request.OrderBy,
                    Descending = request.Descending,
                    AllUsers = request.ShowAllUserData
                };
            if (Count != 0)
            {
                req.Count = Count;
            }
            ListGatewaysResponse ret = await Http.PostAsJson<ListGatewaysResponse>("api/Gateway/List", req, request.CancellationToken);
            if (ret.Gateways != null)
            {
                return new ItemsProviderResult<GXGateway>(ret.Gateways, ret.Count);
            }
        }
        catch (TaskCanceledException)
        {
            //Let the table component handle this.
            throw;
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
        return default;
    }

    /// <summary>
    /// Run selected gateway.
    /// </summary>
    public async void OnRun()
    {
        try
        {
            Notifier.ClearStatus();
            Notifier.ProgressStart();
            if (table?.Active == null)
            {
                throw new Exception(Gurux.DLMS.AMI.Client.Properties.Resources.NoItemIsSelected);
            }
            HttpResponseMessage response = await Http.GetAsync(string.Format("api/Gateway/Run?id={0}", table.Active.Id));
            ClientHelpers.ValidateStatusCode(response);
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
        finally
        {
            Notifier?.ProgressEnd();
        }
    }

    /// <summary>
    /// Add new gateway.
    /// </summary>
    public void OnAdd()
    {
        try
        {
            ClientHelpers.NavigateTo(NavigationManager, Notifier, "/Gateway/Add");
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    /// <summary>
    /// Edit gateway.
    /// </summary>
    public void OnEdit()
    {
        try
        {
            Notifier.ClearStatus();
            if (table?.Active == null)
            {
                throw new Exception(Gurux.DLMS.AMI.Client.Properties.Resources.NoItemIsSelected);
            }
            ClientHelpers.NavigateTo(NavigationManager, Notifier, "/Gateway/Edit/" + table?.Active.Id);
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }


    /// <summary>
    /// Remove selected gateway.
    /// </summary>
    public void OnRemove()
    {
        try
        {
            if (table == null || !table.SingleOrDefault().Any())
            {
                throw new Exception(Gurux.DLMS.AMI.Client.Properties.Resources.NoItemIsSelected);
            }
            DeleteConfirmation?.Show(table.SingleOrDefault().Select(s => s.Name).ToArray());
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    /// <summary>
    /// Delete the selected gateways.
    /// </summary>
    public async Task OnDeleteConfirmation(ConfirmArgs args)
    {
        try
        {
            if (table != null && args.Confirm)
            {
                RemoveGateway req = new RemoveGateway()
                    {
                        Delete = args.Delete,
                        Ids = table.SingleOrDefault().Select(w => w.Id).ToArray()
                    };
                await Http.PostAsJson<RemoveGatewayResponse>("api/Gateway/Delete", req);
            }
        }
        catch (AccessTokenNotAvailableException exception)
        {
            exception.Redirect();
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }
}

